package com.FaxFreeOnline.server.auth;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import com.FaxFreeOnline.server.commons.PropertiesManager;
import com.FaxFreeOnline.server.commons.ReturnResult;
import com.FaxFreeOnline.server.commons.ReturnType;

public class Identification {
	
	private String username = "";
	private String password = "";
	private String url = "";

	public Identification() {
		super();
		
		username = PropertiesManager.getInstance().getPropertie("identification.username");
		password = PropertiesManager.getInstance().getPropertie("identification.password");
		url = PropertiesManager.getInstance().getPropertie("identification.url.authentification");
		
	}
	
	
	public ReturnResult process() throws Exception {
		
		
		    // Construct data
		    String data = URLEncoder.encode("login", "UTF-8") + "=" + URLEncoder.encode(username,"UTF-8");
		    data += "&" + URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");

		    // Send data
		    URL url = new URL(this.url);
		    URLConnection conn = url.openConnection();
		    conn.setDoOutput(true);
		    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
		    wr.write(data);
		    wr.flush();

		    // Get the response
		    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		    String line;
		    
		    while ((line = rd.readLine()) != null) {
		        System.out.println(line);
		    }
		    wr.close();
		    rd.close();
		
		return new ReturnResult(ReturnType.SUCCESS, null);
	}
	
	
	
}
